package com.se.xb.service;

import com.se.xb.entity.User;
import com.se.xb.entity.dto.PageResult;
import com.se.xb.entity.dto.SearchDto;
import com.se.xb.utils.DataSourceUtils;
import com.se.xb.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserService {
    UserDao userDao = new UserDao();
    public Integer login(String username, String password) throws SQLException {
        User userByUsername = userDao.getUserByUsername(username);
        if (userByUsername == null) {
            return 0;
        }
        if (!userByUsername.getPassword().equals(password)) {
            return 1;
        }
        return 2;
    }

    public User getUserByUsername(String username) throws SQLException {
        return userDao.getUserByUsername(username);
    }

    public User getUserById(Long id) throws SQLException {
        return userDao.getUserById(id);
    }

    public User getUserByEmail(String email) throws SQLException {
        return userDao.getUserByEmail(email);
    }

    public void updateCurrentUserDetail(User user) throws SQLException {
        userDao.update(user);
    }

    public Integer getFocusCount(Long id) throws SQLException {
       return userDao.getFocusCount(id);
    }

    public void register(User user) throws SQLException {
        userDao.save(user);
    }

    public PageResult<User> getUserList(SearchDto<String> searchDto) throws SQLException {
        ArrayList<String> queryList = searchDto.getQueryList();
        String s = queryList.get(0);
        Integer currentPage = searchDto.getCurrentPage();
        List<User> userList = userDao.getUserList(currentPage, s);
        PageResult<User> userPageResult = new PageResult<>();
        userPageResult.setRecords(userList);
        userPageResult.setPage(currentPage);
        userPageResult.setPageSize(5);
        userPageResult.setTotal(userDao.getUserCount(s));
        if (userPageResult.getTotal() % 5 == 0) {
            userPageResult.setTotalPage(userPageResult.getTotal() / 5);
        } else {
            userPageResult.setTotalPage(userPageResult.getTotal() / 5 + 1);
        }
        return userPageResult;
    }

    public List<User> getUserList() throws SQLException {
        return userDao.getUserList();
    }

    public PageResult<User> getFocusList(SearchDto<String> searchDto) throws SQLException {
        ArrayList<String> queryList = searchDto.getQueryList();
        String s = queryList.get(0);
        Integer currentPage = searchDto.getCurrentPage();
        List<User> userList = userDao.getFocusList(currentPage, s);
        PageResult<User> userPageResult = new PageResult<>();
        userPageResult.setRecords(userList);
        userPageResult.setPage(currentPage);
        userPageResult.setPageSize(5);
        userPageResult.setTotal(userDao.getFocusCount(Long.parseLong(s)));
        if (userPageResult.getTotal() % 5 == 0) {
            userPageResult.setTotalPage(userPageResult.getTotal() / 5);
        } else {
            userPageResult.setTotalPage(userPageResult.getTotal() / 5 + 1);
        }
        return userPageResult;
    }

    public List<User> getFocusList(Long id) throws SQLException {
        return userDao.getFocusList(id);
    }

    public void focus(Long userId, Long focusId) throws SQLException {
        userDao.focus(userId, focusId);
    }

    public void unfocus(Long userId, Long focusId) throws SQLException {
        userDao.unfocus(userId, focusId);
    }

}
